package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.Trip;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class UserTripsAdapter extends BaseTripAdapter implements ObservableAdapter {

    private LayoutInflater mInflater;
    private boolean mIsOwner;
    private ButtonRowClickHandler mClickListener;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver mResourcesObserver;
    private Handler mHandler = new Handler();
    private Context mContext;

    public UserTripsAdapter(Context context, RemoteResourceManager rrm, ButtonRowClickHandler clickListener, boolean isOwner){
        super(context);
        mInflater = LayoutInflater.from(context);
        mClickListener = clickListener;
        mRrm = rrm;
        mIsOwner = isOwner;
        mResourcesObserver = new RemoteResourceManagerObserver();
        mRrm.addObserver(mResourcesObserver);
        mContext = context;
    }

    public void removeObserver() {
        mHandler.removeCallbacks(mUpdatePhotos);
        mRrm.deleteObserver(mResourcesObserver);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.share_trips_list_item, null);
            holder = new ViewHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.imgTripPhoto);
            holder.name = (TextView) convertView.findViewById(R.id.tvTripName);
            holder.progress = (TextView) convertView.findViewById(R.id.tvTripProgress);
            holder.delete = (TextView) convertView.findViewById(R.id.btnTripShare);
            holder.delete.setOnClickListener(mOnClickListenerTripDelete);
            holder.clickable = (LinearLayout) convertView.findViewById(R.id.llShareClickableArea);
            holder.clickable.setOnClickListener(mOnClickListenerTripInfo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Trip trip = (Trip) getItem(position);
        holder.name.setText(trip.getName());

        if(trip.getProcess()<0){
            int day = Math.abs(trip.getProcess());
            holder.progress.setText(String.format(mContext.getString(R.string.share_trip_progress_upcoming), day));
        }else if(trip.getProcess() != 0){
            holder.progress.setText(String.format(mContext.getString(R.string.share_trip_progress_ongoing), trip.getProcess()));
        }else{
            holder.progress.setText(mContext.getString(R.string.share_trip_progress_done));
        }

        if (trip.getPhoto() != null && !trip.getPhoto().equals("")) {
            final Uri photoUri = Uri.parse(trip.getPhoto());
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
                holder.photo.setImageBitmap(bitmap);
            } catch (IOException e) {
                holder.photo.setImageResource(R.drawable.trip_default_photo);
            }
        }   else {
            holder.photo.setImageResource(R.drawable.trip_default_photo);
        }

        if (trip.getProcess() < 0 && mIsOwner)   {
            holder.delete.setText(R.string.message_activity_item_delete);
        }   else {
            holder.delete.setVisibility(View.GONE);
        }
        holder.delete.setTag(position);
        holder.clickable.setTag(new Integer(position));
        return convertView;
    }

    private class RemoteResourceManagerObserver implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler.post(mUpdatePhotos);
        }
    }

    private Runnable mUpdatePhotos = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };

    @Override
    public void setGroup(Group<Trip> g) {
        super.setGroup(g);

        for (Trip it : g) {
            if (it.getPhoto() != null)  {
                Uri photoUri = Uri.parse(it.getPhoto());
                if (!mRrm.exists(photoUri)) {
                    mRrm.request(photoUri);
                }
            }
        }
    }

    private OnClickListener mOnClickListenerTripDelete = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer position = (Integer) v.getTag();
            mClickListener.onButtonDeleteClick((Trip) getItem(position));
        }
    };

    private OnClickListener mOnClickListenerTripInfo = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer position = (Integer) v.getTag();
            mClickListener.onButtonInfoClick((Trip) getItem(position));
        }
    };

    public interface ButtonRowClickHandler {
        public void onButtonDeleteClick(Trip trip);
        public void onButtonInfoClick(Trip trip);
    }

    private static class ViewHolder {
        LinearLayout clickable;
        ImageView photo;
        TextView name;
        TextView progress;
        TextView delete;
    }
}
